package com.atcc.test.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.atcc.test.domain.Result;
import com.atcc.test.domain.ResultCode;
import com.atcc.test.pojo.BasicUser;
import com.atcc.test.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/")
@Api(value="用于简单测试的页面")
public class SimpleTestController {
	
	@Autowired
	private UserService userService;
	
	@GetMapping("simpleTest/{message}")
	@ApiOperation(value="一个简单测试",notes="一个简单测试")
	public Result<String> simpleTest(@PathVariable(value="message") String message) {
		Result<String> result = new Result<String>(ResultCode.SUCCESS,message);
		return result;
	}
	
	@GetMapping("simpleTest/params")
	@ApiOperation(value="get方式传参数测试",notes="get方式传参数测试")
	public Result<String> simpleParamsTest(
			@RequestParam(value="test") String test,
			@RequestParam(value="message") String message){
		String finalMessage = test+message;
		Result<String> result = new Result<String>(ResultCode.SUCCESS,finalMessage);
		return result;
	}
	
	@PostMapping("simpleTest")
	@ApiOperation(value="POST传参的简单测试",notes="POST传参的简单测试")
	public Result<String> simpleTest(ServletRequest req){
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String s = null;
			if((s=br.readLine())!=null) {
				sb.append(s);
			}
			JSONObject jsonObject = JSONObject.parseObject(sb.toString());
			String message = jsonObject.getString("message");
			Result<String> result = new Result<String>(ResultCode.SUCCESS, message);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		Result<String> result = new Result<String>(ResultCode.SERVICE_ERROR, null);
		return result;
	}
	
	@PostMapping("postTest")
	@ApiOperation(value="post测试",notes="post测试")
	public Result<String> postTest(@RequestBody String testMassage){
		String resultMessage = testMassage;
		Result<String> result = new Result<String>(ResultCode.SUCCESS,resultMessage);
		return result;
	}
	
	
	@PostMapping("ObjectPostTest")
	@ApiOperation(value="post方式传对象测试",notes="post方式传对象测试")
	public Result<BasicUser> objectPostTest(@RequestBody BasicUser testObject) {
		BasicUser simpleObject = testObject;
		Result<BasicUser> result = new Result<BasicUser>(ResultCode.SUCCESS,simpleObject);
		return result;
	}
	
	@GetMapping("simpleTest/getAllBasicUser")
	@ApiOperation(value="查询所有的基础用户信息",notes="查询所有的基础用户信息")
	public Result<List<BasicUser>> queryAllBasicUser(){
		List<BasicUser> message = null;
		List<BasicUser> BasicUserInfo = userService.queryAllBasicUser();
		message = BasicUserInfo; 
		if(message != null) {
			Result<List<BasicUser>> result = new Result<List<BasicUser>>(ResultCode.SUCCESS,message);
			return result;
		}else {
			Result<List<BasicUser>> result = new Result<List<BasicUser>>(ResultCode.SERVICE_ERROR,message);
			return result;
		}
	}

}
